package com.hw.common.framework.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.filter.OncePerRequestFilter;

/*********
 * <pre>
 * 类名称：CORSFilter
 * 类描述：跨域资源共享过滤器
 * 创建人：JokenWang
 * 创建时间：2015年9月17日 下午7:42:58
 * </pre>
 * 
 * @version 1.0.0
 */
@WebFilter(urlPatterns = "/*")
public class CORSFilter extends OncePerRequestFilter implements InitializingBean
{
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
    {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        
        filterChain.doFilter(request, response);
    }
}
